package com.example.activitydemo
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
    private lateinit var resultTv: TextView
    private val getActivity3Result = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == RESULT_OK) {
            val returnData = result.data?.getStringExtra("return_data")
            resultTv.text = "返回结果：$returnData"
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        resultTv = findViewById(R.id.result_tv)
        val btn2 = findViewById<Button>(R.id.btn2)
        val btn3 = findViewById<Button>(R.id.btn3)
        val btn4 = findViewById<Button>(R.id.btn4)
        val btn5 = findViewById<Button>(R.id.btn5)

        // 启动Activity2（正向传参）
        btn2.setOnClickListener {
            val intent = Intent(this, Activity2::class.java)
            intent.putExtra("message", "来自MainActivity的消息")
            startActivity(intent)
        }
        // 启动Activity3（返回数据）
        btn3.setOnClickListener {
            val intent = Intent(this, Activity3::class.java)
            getActivity3Result.launch(intent)
        }
        // 启动Activity4（相机）
        btn4.setOnClickListener {
            val intent = Intent(this, Activity4::class.java)
            startActivity(intent)
        }
        // 启动Activity5（秒表）
        btn5.setOnClickListener {
            val intent = Intent(this, Activity5::class.java)
            startActivity(intent)
        }
    }
}